//
//  WeatherView.m
//  edgeDemo
//
//  Created by dllo on 16/3/7.
//  Copyright © 2016年 徐宏飞. All rights reserved.
//

#import "WeatherView.h"
#import "GETWeatherInfoTool.h"
#import "WeahterModel.h"

@interface WeatherView ()

@property (weak, nonatomic) IBOutlet UILabel *weatherInfo; //天气状况
@property (weak, nonatomic) IBOutlet UILabel *temp;        //现在温度
@property (weak, nonatomic) IBOutlet UILabel *date;        //日期

@property (weak, nonatomic) IBOutlet UILabel *aqi;         //PM2.5
@property (weak, nonatomic) IBOutlet UILabel *pollution;   //污染情况

@property (weak, nonatomic) IBOutlet UILabel *tempLH;      //温差

@property (weak, nonatomic) IBOutlet UILabel *sunEXP;      //防晒指数

@property (weak, nonatomic) IBOutlet UILabel *carwashEXP;  //洗车指数

@property (weak, nonatomic) IBOutlet UIImageView *weatherImage; //天气图


@end




@implementation WeatherView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"WeatherView" owner:self options:nil];
        self = [array firstObject];
        self.frame = frame;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:[UIImage imageNamed:@"user"] forState:UIControlStateNormal];
        btn.frame = CGRectMake(13, 8, 36, 35);
        [self addSubview:btn];
        [btn addTarget:self action:@selector(buttonAc:) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return self;
}

- (void)buttonAc:(UIButton *)btn{

    [self.myDelegate showLeftView];
}


#pragma mark -- 获取数据
- (void)GetData{
    
    NSString *httpUrl = @"http://apis.baidu.com/apistore/weatherservice/recentweathers";

    
    [GETWeatherInfoTool getWeahterInfoWithRequest:httpUrl andHttpArg:self.httpArg Block:^(id result) {
//        NSLog(@"%@", result);
        NSMutableDictionary *dic = [result objectForKey:@"retData"];
        WeahterModel *model = [[WeahterModel alloc] initWithDictionary:[dic objectForKey:@"today"]];
        
        //天气状况
        self.weatherInfo.text = model.type;
        //当前温度
        self.temp.text = [model.curTemp substringToIndex:model.curTemp.length-1];
        //日期
        self.date.text = [model.date stringByAppendingString:model.week];
        //pm2.5
        self.aqi.text = model.aqi;
        //污染情况
        self.pollution.text = [self getPollutionStateWithApi:model.aqi];
        //温度范围
        NSString *first = [dic objectForKey:@"city"];
        NSString *second = [model.lowtemp substringToIndex:model.curTemp.length];
        NSString *third = model.hightemp;
        self.tempLH.text = [NSString stringWithFormat:@"%@%@~%@", first, second, third];
        //防晒指数
        self.sunEXP.text = [NSString stringWithFormat:@"%@:%@", model.index[1].name, model.index[1].index];
        //洗车指数
        self.carwashEXP.text = [NSString stringWithFormat:@"%@:%@", model.index[4].name, model.index[4].index];
        //天气图
        self.weatherImage.image = [UIImage imageNamed:model.type];
  
        
        
    } error:^(NSError *errorBlock) {
        
        
    }];
    
    
    
    
}


#pragma mark -- 根据aqi情况判断空气质量
- (NSString *)getPollutionStateWithApi:(NSString *)aqi{

    if (aqi.intValue < 35) {
        return @"优";
    } else if (aqi.intValue < 75 && aqi.intValue >= 35) {
        return @"良";
    } else if (aqi.intValue < 115 && aqi.intValue >= 75) {
        return @"轻度污染";
    } else if (aqi.intValue < 150 && aqi.intValue >= 115) {
        return @"中度污染";
    } else if (aqi.intValue < 250 && aqi.intValue >= 150) {
        return @"重度污染";
    } else {
        return @"严重污染";
    }
    
//    空气质量等级	24小时PM2.5平均值标准值
//    优0~35μg/m³
//    良35~75μg/m³
//    轻度污染75~115μg/m³
//    中度污染115~150μg/m³
//    重度污染150~250μg/m³
//    严重污染大于250μg/m³及以上
    
    
    
}







@end
